package com.atguigu.gmallre.mock.service.impl;

import com.atguigu.gmallre.mock.bean.*;
import com.atguigu.gmallre.mock.config.AppConfig;
import com.atguigu.gmallre.mock.constant.GmallConstant;
import com.atguigu.gmallre.mock.mapper.*;
import com.atguigu.gmallre.mock.service.*;
import com.atguigu.gmallre.mock.service.adv.impl.AdvServiceImpl;
import com.atguigu.mock.util.RandomNum;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.math.BigDecimal;
import java.util.*;

/**
 * <p>
 * 优惠券表 服务实现类
 * </p>
 *
 * @author zhangchen
 * @since 2020-02-26
 */
@Service
public class CouponInfoServiceImpl extends AdvServiceImpl<CouponInfoMapper, CouponInfo> implements CouponInfoService {


    List  tmCouponList =new ArrayList();

    List<CouponInfo>  couponListCache=new ArrayList();


    @Autowired
    CouponRangeMapper couponRangeMapper;

    @PostConstruct
    public void initCouponCache(){

        List<CouponRange> couponRangeList = couponRangeMapper.selectList(new QueryWrapper<CouponRange>());

        for (CouponRange couponRange : couponRangeList) {
            CouponInfo couponInfo = this.getById(couponRange.getCouponId(), true);
            couponInfo.setRangeId(couponRange.getRangeId());
            couponListCache.add(couponInfo);
        }

    }


    @PostConstruct
    public void initTmCouponCache(){

    }

    public  CouponUse  takeBestCouponForOrder(OrderInfo orderInfo){
        List<OrderDetail> orderDetailList = orderInfo.getOrderDetailList();


        //1 订单中的而每个商品 去匹配每个规则 ,获得orderCouponMap的对象，包含了该订单可能涉及的购物券
        Map<Long, CouponUse> orderCouponMap=new HashMap<>();
        for (OrderDetail orderDetail : orderDetailList) {
            for (CouponInfo couponInfo : couponListCache) {
                Long couponKey = couponInfo.getId();
                if(couponInfo.getRangeType().equals(GmallConstant.COUPON_RANGE_TYPE_CATEGORY3)&&couponInfo.getRangeId().equals(orderDetail.getSkuInfo().getCategory3Id())){

                    CouponUse couponUse  = orderCouponMap.getOrDefault(couponKey, new CouponUse());
                    couponUse.setCouponInfo(couponInfo);
                    couponUse.setOrderId(orderInfo.getId());
                    couponUse.addOrderDetail(orderDetail);
                    orderCouponMap.put(couponKey,couponUse);
                }else if(couponInfo.getRangeType().equals(GmallConstant.COUPON_RANGE_TYPE_TRADEMARK)&&couponInfo.getRangeId().equals(orderDetail.getSkuInfo().getTmId())){
                    CouponUse couponUse   = orderCouponMap.getOrDefault(couponKey, new CouponUse());
                    couponUse.setCouponInfo(couponInfo);
                    couponUse.setOrderId(orderInfo.getId());
                    couponUse.addOrderDetail(orderDetail);
                    orderCouponMap.put(couponKey,couponUse);
                }else if(couponInfo.getRangeType().equals(GmallConstant.COUPON_RANGE_TYPE_SPU)&&couponInfo.getRangeId().equals(orderDetail.getSkuInfo().getSpuId())) {
                     CouponUse couponUse   = orderCouponMap.getOrDefault(couponKey, new CouponUse());
                    couponUse.setCouponInfo(couponInfo);
                    couponUse.setOrderId(orderInfo.getId());
                    couponUse.addOrderDetail(orderDetail);
                    orderCouponMap.put(couponKey,couponUse);
                }
            }
        }
        //2 比较购物券是否达到要求的额度，去取优惠力度最大的
        CouponUse bestCoupon=null;
        BigDecimal mostBenefitAmount=BigDecimal.ZERO;
        for (  CouponUse couponUse  : orderCouponMap.values()) {
            if(couponUse.checkRequireCondition()){
                BigDecimal benefitAmount = couponUse.calcReduceAmount();
                if(benefitAmount.compareTo(mostBenefitAmount)>0){
                    mostBenefitAmount=benefitAmount;
                    bestCoupon=couponUse;
                }
            }
        }


        return  bestCoupon;


    }

    public void updateRecentlyDate(){
        List<CouponInfo> couponInfoList = list();

        for (CouponInfo couponInfo : couponInfoList) {

            Date stDate=  DateUtils.addDays(AppConfig.mock_date, 0-(   RandomNum.getRandInt(1,20)));
            Date edDate=  DateUtils.addDays(AppConfig.mock_date,  (   RandomNum.getRandInt(1,20)));



            couponInfo.setCreateTime(stDate);
            couponInfo.setExpireTime(edDate);
        }
        saveOrUpdateBatch(couponInfoList) ;
    }



}
